package org.lazy.generate.factory;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.lazy.generate.common.MethodNameConstants;
import org.lazy.util.StringUtil;

public class FileFactory extends MethodNameConstants{
	protected final static String t1 = "\t";
	protected final static String t2 = "\t\t";
	protected final static String t3 = "\t\t\t";
	protected final static String t4 = "\t\t\t\t";
	protected final static String t5 = "\t\t\t\t\t";
	protected final static String t6 = "\t\t\t\t\t\t";
	protected final static String t7 = "\t\t\t\t\t\t\t";
	protected final static String t8 = "\t\t\t\t\t\t\t\t";
	protected final static String t9 = "\t\t\t\t\t\t\t\t\t";
	protected final static String t10 = "\t\t\t\t\t\t\t\t\t\t";

	/**
	 * 生成类源文件
	 */
	public static File createTargetClass(String targetClassFilePath,
			String targetClassName) {
		File path = new File(targetClassFilePath);
		if (!path.isDirectory()) {
			path.mkdirs();
		}
		File dtoFile = new File(targetClassFilePath + "\\" + targetClassName
				+ ".java");
		if (dtoFile.isFile()) {
			dtoFile.delete();
		}
		try {
			dtoFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dtoFile;
	}

	/**
	 * 生成MapperXml文件
	 */
	public static File createTargetMapperXml(String targetMapperXmlPath,
			String targetMapperXmlName) {
		File path = new File(targetMapperXmlPath);
		if (!path.isDirectory()) {
			path.mkdirs();
		}
		File dtoFile = new File(targetMapperXmlPath + "\\"
				+ targetMapperXmlName + ".xml");
		if (dtoFile.isFile()) {
			dtoFile.delete();
		}
		try {
			dtoFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dtoFile;
	}
	
	/**
	 * 生成Entity类的Name
	 */
	public static String createEntityClassName(String targetName,
			String tableName) {
		if (StringUtil.isBlank(targetName)) {
			targetName = "";
			if (StringUtil.isNotBlank(tableName)) {
				String[] strArray = tableName.split("_");
				int length = strArray.length;
				for (int i = 0; i < length; i++) {
					targetName += strArray[i].substring(0, 1).toUpperCase()
							+ strArray[i].substring(1);
				}
			}
		}
		return targetName;
	}
	/**
	 * 生成ResultMap的id
	 */
	public static String createBaseResultMapID(String entityClassName,
			String tableName) {
		return "BaseResultMap";
	}
	/**
	 * 生成ParameterMap的id
	 */
	public static String createBaseParameterMapID(String entityClassName,
			String tableName) {
		return "BaseParameterMap";
	}
	/**
	 * 生成EntityCondition类的Name
	 */
	public static String createEntityConditionClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "Condition";
	}

	/**
	 * 生成MapperXml的Name
	 */
	public static String createMapperXmlName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "Mapper";
	}

	/**
	 * 生成MapperXml的Name
	 */
	public static String createMapperXmlExtName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "MapperExt";
	}
	/**
	 * 生成Dao接口类的Name
	 */
	public static String createDaoClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "Dao";
	}

	/**
	 * 生成Dao接口实现类DaoImpl的Name
	 */
	public static String createDaoImplClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "DaoImpl";
	}

	/**
	 * 生成DaoExt接口类的Name
	 */
	public static String createDaoExtClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "DaoExt";
	}

	/**
	 * 生成DaoExt接口实现类DaoImplExt的Name
	 */
	public static String createDaoImplExtClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "DaoImplExt";
	}

	/**
	 * 生成Service接口类的Name
	 */
	public static String createServiceClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "Service";
	}

	/**
	 * 生成Service接口实现类ServiceImpl的Name
	 */
	public static String createServiceImplClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName)
				+ "ServiceImpl";
	}

	/**
	 * 生成ServiceExt接口类的Name
	 */
	public static String createServiceExtClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName) + "ServiceExt";
	}

	/**
	 * 生成ServiceExt接口实现类ServiceImplExt的Name
	 */
	public static String createServiceImplExtClassName(String entityClassName,
			String tableName) {
		return createEntityClassName(entityClassName, tableName)
				+ "ServiceImplExt";
	}

	/**
	 * 根据表字段名生成属性名
	 */
	public static String createFieldName(String columnName) {
		String newName = "";
		if (StringUtil.isNotBlank(columnName)) {
			String[] strArray = columnName.toLowerCase().split("_");
			int length = strArray.length;
			for (int i = 0; i < length; i++) {
				if (i == 0) {
					newName += strArray[i].substring(0, 1).toLowerCase()
							+ strArray[i].substring(1);
				} else {
					newName += strArray[i].substring(0, 1).toUpperCase()
							+ strArray[i].substring(1);
				}
			}
		}
		return newName;
	}

	/**
	 * 获取dao类全名
	 */
	public static String createFullDaoClassName(String daoPackage,
			String daoClassName) {
		return daoPackage + "." + daoClassName;
	}

	/**
	 * 获取dao类全名
	 */
	public static String createFullServiceClassName(String servicePackage,
			String serviceClassName) {
		return servicePackage + "." + serviceClassName;
	}

	/**
	 * 获取Entity类全名
	 */
	public static String createFullEntityClassName(String entityPackage,
			String entityClassName) {
		return entityPackage + "." + entityClassName;
	}

	/**
	 * 获取EntityCondition类全名
	 */
	public static String createFullEntityConditionClassName(
			String entityConditionPackage, String entityConditionClassName) {
		return entityConditionPackage + "." + entityConditionClassName;
	}

	/**
	 * 判断java文件是否存在
	 */
	public static boolean isExistJavaFile(String packagePath, String className) {
		boolean flag = false;
		File javaFile = new File(packagePath + "\\" + className + ".java");
		if (javaFile.exists()) {
			flag = true;
		}
		return flag;
	}
	/**
	 * 判断java文件是否存在
	 */
	public static boolean isExistXmlFile(String packagePath, String xmlFileName) {
		boolean flag = false;
		File javaFile = new File(packagePath + "\\" + xmlFileName + ".xml");
		if (javaFile.exists()) {
			flag = true;
		}
		return flag;
	}


	public static void writeImportClass(PrintWriter pw,
			Set<String> importClasses) {
		if (importClasses.size() > 0) {
			List<String> list = new ArrayList<String>(importClasses);
			Collections.sort(list);
			for(String s:list){
				pw.println("import " + s + ";");
			}
		}
	}
	
	/**
	 * 小写首字母
	 * @param str
	 * @return
	 */
	public static String lowerCaseFirstAlphabet(String str){
		if(str!=null){
			str = str.substring(0, 1).toLowerCase()+str.substring(1);
		}
		return str;
	}
	/**
	 * 大写首字母
	 * @param str
	 * @return
	 */
	public static String upperCaseFirstAlphabet(String str){
		if(str!=null){
			str = str.substring(0, 1).toUpperCase()+str.substring(1);
		}
		return str;
	}
}
